package com.hqhy.like.front.order.service.impl;

import com.hqhy.like.common.enumerator.ServiceCode;
import com.hqhy.like.common.ex.ServiceException;
import com.hqhy.like.front.order.dao.persist.repository.ITableRepository;
import com.hqhy.like.front.order.pojo.entity.Table;
import com.hqhy.like.front.order.pojo.param.TableUpdateInfoParam;
import com.hqhy.like.front.order.pojo.vo.TableStandardVO;
import com.hqhy.like.front.order.service.ITableService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class TableServiceImpl implements ITableService {

    @Autowired
    private ITableRepository tableRepository;

    public TableServiceImpl() {
        log.debug("创建业务类对象：TableServiceImpl");
    }

    @Override
    public void updateInfoById(Integer tableId, TableUpdateInfoParam tableUpdateInfoParam) {
        log.debug("开始处理【根据桌台ID修改桌台数据】的业务，桌台id: {}，桌台更新信息类：{}", tableId, tableUpdateInfoParam);
        TableStandardVO queryResult = tableRepository.getStandardById(tableId);
        if (queryResult == null){
            String message = "更新桌台数据失败，桌台数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Table table = new Table();
        BeanUtils.copyProperties(tableUpdateInfoParam,table);
        table.setId(tableId);
        int rows = tableRepository.updateById(table);
        if (rows != 1){
            String message = "修改桌台数据失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public List<TableStandardVO> listAllByIsAvailable() {
        log.debug("开始处理【查询所有空闲的桌台】的业务");
        return tableRepository.listAllByIsAvailable();
    }

    @Override
    public void updateInfoByName(String tableName, TableUpdateInfoParam tableUpdateInfoParam) {
        log.debug("开始处理【根据桌台名修改桌台数据】的业务，桌台名: {}，桌台更新信息类：{}", tableName, tableUpdateInfoParam);
        TableStandardVO queryResult = tableRepository.getStandardByName(tableName);
        if (queryResult == null){
            String message = "更新桌台数据失败，桌台数据不存在！";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        Table table = new Table();
        BeanUtils.copyProperties(tableUpdateInfoParam,table);
        table.setId(queryResult.getId());
        int rows = tableRepository.updateById(table);
        if (rows != 1){
            String message = "修改桌台数据失败，服务器忙，请稍后再尝试！";
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

}
